package com.cwl;

import java.util.Collections;
import java.util.concurrent.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class GetThreadValue {
    private static int value;
    private static final ReadWriteLock lock = new ReentrantReadWriteLock();
    private static final CyclicBarrier cyclicBarrier = new CyclicBarrier(2);
    private static final CountDownLatch countDownLatch = new CountDownLatch(1);
    private static final Semaphore semaphore = new Semaphore(1);
    public static void main(String[] args) throws ExecutionException, InterruptedException, BrokenBarrierException {
        // 方法一：
        /*FutureTask<Integer> future = new FutureTask<>(GetThreadValue::addValue);
        new Thread(future).start();
        Integer integer = future.get();*/

        // 方法二：
        /*new Thread(GetThreadValue::addValue).start();
        Thread.sleep(1000);
        Integer integer = value;*/

        // 方法三：
        /*new Thread(()->{
            try {
                addValue();
                cyclicBarrier.await();
            } catch (InterruptedException | BrokenBarrierException e) {
                e.printStackTrace();
            }
        }).start();
        cyclicBarrier.await();
        Integer integer = value;*/

        // 方法四：
        /*new Thread(()->{
            addValue();
            countDownLatch.countDown();
        }).start();
        countDownLatch.await();
        Integer integer = value;
        System.out.println(integer);*/

        // 方法五：
        /*new Thread(()->{
            try {
                semaphore.acquire();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            addValue();
            semaphore.release();
        }).start();
        Thread.sleep(20);
        semaphore.acquire();
        Integer integer = value;
        System.out.println(integer);
        semaphore.release();*/
    }

    static int addValue(){
        return ++value;
    }
}
